package com.maddox.libcommon.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.google.android.material.button.MaterialButton;
import com.maddox.libcommon.R;

public class EmptyView extends LinearLayoutCompat {

    private ImageView emptyIcon;
    private TextView emptyTitle;
    private MaterialButton emptyAction;

    public EmptyView(@NonNull Context context) {
        this(context, null);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_empty_view, this, true);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        emptyIcon = findViewById(R.id.empty_icon);
        emptyTitle = findViewById(R.id.empty_title);
        emptyAction = findViewById(R.id.empty_action);

    }

    public void setEmptyIcon(@DrawableRes int resId) {
        emptyIcon.setImageResource(resId);
    }

    public void setEmptyTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            emptyTitle.setVisibility(GONE);
        } else {
            emptyTitle.setText(title);
            emptyTitle.setVisibility(VISIBLE);
        }
    }

    public void setEmptyAction(String text, View.OnClickListener listener) {
        if (TextUtils.isEmpty(text)) {
            emptyAction.setVisibility(GONE);
        } else {
            emptyAction.setText(text);
            emptyAction.setVisibility(VISIBLE);
            emptyAction.setOnClickListener(listener);
        }

    }
}
